package cn.iocoder.yudao.module.iot.config;

import cn.iocoder.yudao.framework.security.config.AuthorizeRequestsCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;

/**
 * IoT 模块的 Security 配置
 */
@Configuration
public class IotSecurityConfiguration {

    @Bean
    public AuthorizeRequestsCustomizer iotAuthorizeRequestsCustomizer() {
        return new AuthorizeRequestsCustomizer() {
            @Override
            public void customize(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry registry) {
                // IoT 模块的 API 白名单 - 只包含无需登录的接口
                registry.requestMatchers(buildAppApi("/iot/user/login")).permitAll() // 用户登录
                        .requestMatchers(buildAppApi("/iot/user/register")).permitAll() // 用户注册
                        .requestMatchers(buildAppApi("/iot/device/**")).permitAll() // 设备相关接口
                        .requestMatchers(buildAppApi("/iot/room/**")).permitAll() // 房间相关接口
                        .requestMatchers(buildAppApi("/iot/scene/**")).permitAll() // 场景相关接口
                        .requestMatchers(("/admin-api/iot/device/simple-list")).permitAll() // 设备列表接口
                        .requestMatchers(("/admin-api/iot/product/user-product-name")).permitAll() // 产品名接口
                        .requestMatchers(("/admin-api/iot/device/device-name")).permitAll() // 设备名接口
                        .requestMatchers(("/admin-api/iot/product/simple-list")).permitAll() // 产品列表接口
                        .requestMatchers(("/admin-api/iot/device/simple-list")).permitAll() // 设备列表接口
                        .requestMatchers(("/admin-api/iot/device/device-name")).permitAll() // 设备名接口
                        .requestMatchers(("/admin-api/iot/scene/**")).permitAll() // 场景接口
                        .requestMatchers(("/admin-api/iot/product/user-products/slave")).permitAll() // 
                        .requestMatchers(("/admin-api/iot/product/user-products/master")).permitAll() // 主产品接口
                        .requestMatchers(("/admin-api/iot/device/by-product")).permitAll() // 设备名接口
                        .requestMatchers(("/admin-api/iot/scene/condition/**")).permitAll() // 场景条件接口
                        .requestMatchers("/admin-api/iot/scene/all-scene-actions").permitAll() // 管理端场景动作接口
                        .requestMatchers("/admin-api/iot/scene/user-devices/**").permitAll() // 管理端用户设备接口
                        .requestMatchers("/admin-api/iot/scene/device-nickname/**").permitAll() // 设备昵称接口
                        .requestMatchers("/admin-api/iot/scene/*/status").permitAll() // 场景状态更新接口
                        .requestMatchers("/admin-api/iot/scene/*").permitAll() // 场景更新接口
                        .requestMatchers("/admin-api/iot/scene/condition/*").permitAll() // 场景条件更新接口
                        .requestMatchers("/admin-api/iot/scene/action/*").permitAll() // 场景动作更新接口
                        .requestMatchers("/admin-api/iot/device/admin/user-device-services").permitAll()  // 管理端用户设备服务接口
                        .requestMatchers("/rpc-api/iot/**").permitAll(); // rpc-api场景相关接口
            }
        };
    }
} 